/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.pdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import org.ipti.br.tag.util.ServiceUtil;

/**
 *
 * @author FelipeMSX
 */
public abstract class AbstractPdf implements Serializable {
        
    private String name;
    private Document document; 
    private final Font fontTitle = new Font(Font.FontFamily.TIMES_ROMAN,13, Font.BOLD);
    private final Font fontTextNORMAL  = new Font(Font.FontFamily.TIMES_ROMAN,10,Font.NORMAL);
    private final Font fontTextBold  = new Font(Font.FontFamily.TIMES_ROMAN,10,Font.BOLD);
    private final float spaceToFirstLine = 20;
    private Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
    private Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD);
    private Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
    private Font textF = new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL);
  
    public void init(String name) throws FileNotFoundException, DocumentException {
        document = new Document(PageSize.A4,31,31,31,31);
        this.create(name.replaceAll("\\W", ""));
        document.open();
    }
    
    private void create(String name) throws FileNotFoundException, DocumentException {
        this.name = name;
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        FileOutputStream file = new FileOutputStream(new File(externalContext.getRealPath("resources/pdf/"+name+".pdf")));
        PdfWriter.getInstance(document, file);
   }
     
   public void close() throws IOException{
        document.close();
   }
    
    public void newPage(){
        getDocument().newPage();
    }
      
    public void export(){
        FacesContext facesContext = ServiceUtil.getFacesContext();
        ExternalContext externalContext = facesContext.getExternalContext();
        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

        File file = new File(externalContext.getRealPath(("resources/pdf/"+name+".pdf").replaceAll("//W", "")));
        BufferedInputStream input = null;
        BufferedOutputStream output = null;
        final int DEFAULT_BUFFER_SIZE = 10240;
        try {
            input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
            response.reset();
            response.setHeader("Content-Type", "application/pdf");
            response.setHeader("Content-Length", String.valueOf(file.length()));
            response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
            output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            output.flush();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                output.close();
                input.close();
                file.delete();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        facesContext.responseComplete();
   }
   
   public void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
   }
      
   public Document getDocument() {
       return document;
   }
    
   public void setDocument(Document document) {
       this.document = document;
   }

   public Font getFontTitle() {
       return fontTitle;
   }
   
   public Font getFontTextNORMAL() {
       return fontTextNORMAL; 
   }
   
   public Font getFontTextBold() {
       return fontTextBold; 
   }
   
   public float getSpaceToFirstLine() {
       return spaceToFirstLine;  
   }

    public Font getCatFont() {
        return catFont;
    }

    public void setCatFont(Font catFont) {
        this.catFont = catFont;
    }

    public Font getSubFont() {
        return subFont;
    }

    public void setSubFont(Font subFont) {
        this.subFont = subFont;
    }

    public Font getSmallBold() {
        return smallBold;
    }

    public void setSmallBold(Font smallBold) {
        this.smallBold = smallBold;
    }

    public Font getTextF() {
        return textF;
    }

    public void setTextF(Font textF) {
        this.textF = textF;
    }

}
